package com.qch.concurrent.demo_03_cyclicBarrier;

import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Semaphore;

/**
 * @Author qch
 * @Date 2021/8/2
 */
public class MyThread extends Thread {
    private final CyclicBarrier barrier;
    private final Random random = new Random();

    public MyThread(String name, CyclicBarrier cyclicBarrier) {
        super(name);
        this.barrier = cyclicBarrier;
    }

    @Override
    public void run() {

        try {
            System.out.println(Thread.currentThread().getName()+"向公司出发");
            Thread.sleep(random.nextInt(2000));
            System.out.println(Thread.currentThread().getName()+"已经到达公司");
            //等待其他线程该阶段结束
            barrier.await();

            System.out.println(Thread.currentThread().getName()+"开始笔试");
            Thread.sleep(random.nextInt(2000));
            System.out.println(Thread.currentThread().getName()+"笔试结束");
            barrier.await();

            System.out.println(Thread.currentThread().getName()+"开始面试");
            Thread.sleep(random.nextInt(2000));
            System.out.println(Thread.currentThread().getName()+"面试结束");

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }
}
